package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.vo.common.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LT
 * @create 2024-03-11-18:54
 */
@RestController
@Tag(name = "角色管理模块")
//@CrossOrigin(allowCredentials = "true",originPatterns = "*",allowedHeaders = "*")
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/findByKeyword/{pageNum}/{pageSize}")
    public Result findByKeyword(@PathVariable("pageNum") Integer pageNum,
                                @PathVariable("pageSize") Integer pageSize,
                                @RequestParam(required = false) String keyword
    ) {
        PageInfo<SysRole> page = sysRoleService.findByKeyword(pageNum, pageSize, keyword);
        return Result.ok(page);
    }

    @PostMapping("/addSysRole")
    public Result addSysRole(@RequestBody SysRole sysRole) {
        sysRoleService.addSysRole(sysRole);
        return Result.ok();
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer rid) {
        SysRole sysRole = sysRoleService.findById(rid);
        return Result.ok(sysRole);
    }

    @PutMapping("/updateById")
    public Result updateById(@RequestBody SysRole sysRole) {
        sysRoleService.updateById(sysRole);
        return Result.ok();
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Integer rid) {
        sysRoleService.deleteById(rid);
        return Result.ok();
    }

    @GetMapping("/findAll")
    public Result findAll() {
        List<SysRole> sysRoleList = sysRoleService.findAll();
        return Result.ok(sysRoleList);
    }

    @GetMapping("/findAllRoles/{userId}")
    public Result findAllRoles(@PathVariable("userId") Integer userId) {
        List<SysRole> sysRoleList = sysRoleService.findAllRoles(userId);
        return Result.ok(sysRoleList);
    }

    @PutMapping("/assignMenus2Role/{roleId}")
    public Result AssignMenus2Role(@PathVariable("roleId") Long roleId,
                                   @RequestBody List<Long> menuIds) {
        sysRoleService.assignMenus2Role(roleId, menuIds);
        return Result.ok();
    }


}
